using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace CodePlex.SpaceBlock.UI.Controls
{

    public class ComboBox2 : ComboBox
    {
       
        private const int __textBoxPadding = 4;
      
        private bool _readOnly;
        private readonly TextBox _textBox;
       


        public ComboBox2()
        {
            _textBox = new TextBox();

            // bubble the text-box keypress events up to this control
            // may need to add other events as needed
            _textBox.KeyPress += delegate(object sender, KeyPressEventArgs e)
            {
                this.OnKeyPress(e);
            };
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // disable the auto "drop down" triggered by type-ahead when the control key is also held down
            if (!this.ReadOnly
                && (keyData & Keys.Control) == Keys.Control
                && keyData != Keys.Control
                )
            {
                base.ProcessDialogKey(keyData);
                return true;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }


        new public object SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                if (object.Equals(this.SelectedItem, value))
                {
                    return;
                }
                base.SelectedItem = value;
                this.OnSelectionChangeCommitted(EventArgs.Empty);
            }
        }



        // adapted from http://blogs.msdn.com/jfoscoding/articles/480808.aspx
        public bool ReadOnly
        {
            get
            {
                return _readOnly;
            }
            set
            {
                _readOnly = value;

                if (_readOnly)
                {
                    this.Enabled = false;

                    _textBox.BackColor = SystemColors.Control;
                    _textBox.BorderStyle = BorderStyle.None;
                    _textBox.ForeColor = ForeColor;
                    _textBox.Location = new Point(this.Location.X + __textBoxPadding, this.Location.Y + __textBoxPadding);
                    _textBox.Multiline = true;
                    _textBox.ReadOnly = true;
                    _textBox.Size = new Size(this.Size.Width - SystemInformation.VerticalScrollBarWidth - 2 * __textBoxPadding, this.Size.Height - 2 * __textBoxPadding);
                    _textBox.Text = this.Text;
                   

                    this.Parent.Controls.Add(_textBox);
                    this.Parent.Controls.SetChildIndex(_textBox, this.Parent.Controls.GetChildIndex(this));
                }
                else
                {
                    this.Enabled = true;
                    if (this.Parent != null)
                    {
                        this.Parent.Controls.Remove(_textBox);
                    }
                }
            }
        }

    }
}
